import { defineStore } from 'pinia'
import { ref, computed, watch } from 'vue'
import type { PlayerStore } from "@/store/types";
import { ToneLevel } from '@/enum'
import { PlayDetail } from '../types'

export default defineStore('player', (): PlayerStore => {

    function usePlay() {
        const playDetail = ref<PlayDetail>({
            state: false,// 播放状态 暂停 播放
            progress: 0,// 播放进度条
            toneLevel: 'standard',// 音质等级
            duration: 0,//音乐总时长
            currentTime: 0,//音乐当前播放时间
            currentSongId: null,// 当前播放歌曲的id
        })
        // 播放列表
        const playList = ref([
            {
                "name": "爱的鼓励",
                "id": 29850679,
                "pst": 0,
                "t": 0,
                "ar": [
                    {
                        "id": 3684,
                        "name": "林俊杰",
                        "tns": [],
                        "alias": []
                    }
                ],
                "alia": [
                    "电视剧《一见不钟情》片头曲"
                ],
                "pop": 100,
                "st": 0,
                "rt": null,
                "fee": 1,
                "v": 68,
                "crbt": null,
                "cf": "",
                "al": {
                    "id": 3056951,
                    "name": "新地球 - 人 (Special Edition)",
                    "picUrl": "http://p1.music.126.net/QXZRckFTN5375vdQSyG0jA==/109951166919095160.jpg",
                    "tns": [],
                    "pic_str": "109951166919095160",
                    "pic": 109951166919095170
                },
                "dt": 228813,
                "h": {
                    "br": 320000,
                    "fid": 0,
                    "size": 9155440,
                    "vd": -73174
                },
                "m": {
                    "br": 192000,
                    "fid": 0,
                    "size": 5493281,
                    "vd": -70618
                },
                "l": {
                    "br": 128000,
                    "fid": 0,
                    "size": 3662202,
                    "vd": -68928
                },
                "sq": {
                    "br": 1056317,
                    "fid": 0,
                    "size": 30212450,
                    "vd": -73155
                },
                "hr": null,
                "a": null,
                "cd": "1",
                "no": 9,
                "rtUrl": null,
                "ftype": 0,
                "rtUrls": [],
                "djId": 0,
                "copyright": 1,
                "s_id": 0,
                "mark": 8192,
                "originCoverType": 1,
                "originSongSimpleData": null,
                "tagPicList": null,
                "resourceState": true,
                "version": 68,
                "songJumpInfo": null,
                "entertainmentTags": null,
                "awardTags": null,
                "single": 0,
                "noCopyrightRcmd": null,
                "rtype": 0,
                "rurl": null,
                "mst": 9,
                "cp": 7002,
                "mv": 400016,
                "publishTime": 1419609600007,
                "videoInfo": {
                    "moreThanOne": false,
                    "video": {
                        "vid": "400016",
                        "type": 0,
                        "title": "《超级面对面》独家对话林俊杰预告片",
                        "playTime": 30361,
                        "coverUrl": "http://p1.music.126.net/KtB66mtk2xJBQwvCchQXHw==/2889516558670981.jpg",
                        "publishTime": 1430064000007,
                        "artists": null,
                        "alias": null
                    }
                }
            }
        ])
        const songUrlList = ref([])

        const playNowDetail = computed(() => {
            return playList.value.find(item => item.id === playDetail.value.currentSongId)
        })
        const playNowUrl = computed(() => {
            return songUrlList.value.find(item => item.id === playDetail.value.currentSongId)
        })
        watch(() => playDetail.value.currentSongId, (newVal) => {
            if (!newVal) playDetail.value.currentSongId = playList.value[0].id
            playDetail.value.currentTime = 0
        }, { immediate: true })


        function setPlayState(state) {
            playDetail.value.state = state
        }

        return { setPlayState, playList, playNowDetail, playNowUrl, playDetail, songUrlList }
    }

    function useUtils() {
        const playType = ref(0)

        function setPlayType() {
            playType.value++
            if (playType.value > 3) {
                playType.value = 0
            }
        }

        // 音量
        const volume = ref(20)
        return { playType, setPlayType, volume }
    }

    return { ...useUtils(), ...usePlay() }
}, {
    persist: {
        paths: [
            'playDetail.progress',
            'playDetail.toneLevel',
            'playDetail.duration',
            'playDetail.currentTime',
            'playDetail.currentSongId',
            'playList',
            'playType',
            'volume'
        ],
    }
})
